<?php
namespace app\admin\controller;
use think\Db;//导入数据库类 (DB类)
use think\Controller;
class Jurisdiction extends Common
{


	//管理员设置
	public function index()
    {
//         $subQuery= Db::view('Admin','admin_id,username')
//         ->view('AdminRole','role_id','AdminRole.admin_id=Admin.admin_id','LEFT')
//         ->view('Role','role_name','Role.id=AdminRole.role_id','LEFT')
//         ->buildSql();
        
//         $count=Db::table($subQuery."a")->count();
        
//         $pagesize='3';
//         $page_u=new \util\Page($count,$pagesize);
//         $page_u=$page_u->fpage();
//         $page=input('get.page');
//         $page=isset($page)?$page:"1";
        
        
        //-------------管理员的角色查询
        $data=Db::view('Admin','admin_id,username')
        ->view('AdminRole','role_id','AdminRole.admin_id=Admin.admin_id','LEFT')
        ->view('Role','role_name','Role.id=AdminRole.role_id','LEFT')
        ->paginate(3);
       
        $page_u = $data->render();
    	//$data=db('admin')->page($page,$pagesize)->select();
    	$this->assign('data',$data);
    	$this->assign('page',$page_u);
       	return view('Jurisdiction/index');
      
      
    }

    public function add()
    {
    		
    		return view('Jurisdiction/indexadd');
    }

    public function doadd()
    {
    	if (empty(input('username')&&input('pwd'))) {
    		$this->error('输入信息有误');
    	}
    	$password=md5(input('pwd'));
    	$data = ['username' =>input('username'), 'password' =>$password];
    	db('admin')->insert($data);
    	$this->success('添加成功','admin/Jurisdiction/index');
    }


     public function del()
     {
     	$data=db('admin')->where('admin_id',input('id'))->delete();
     	$this->success('删除成功');
     	
    
     }

    public function edit()
     {
     	$data=db('admin')->where('admin_id',input('id'))->find();
    	$this->assign('data',$data);
       	return view('Jurisdiction/edit');
     }

     public function doedit()
     {
     	if (empty(input('username')&&input('pwd'))) {
    		$this->error('输入信息有误');
    	}
    	$passoword=md5(input('pwd'));
    	$data = ['username' =>input('username'), 'password' =>$passoword];
    	$rus=db('admin')->where('admin_id',input('id'))->update($data);
    	if ($rus>0) {
    		$this->success('修改成功','admin/Jurisdiction/index');
    	}else{
    		$this->error('错误');
    	}
    	
     }


      public function jobset()
     {
        //查询当前用户信息
        
        $data=db('admin')->where('admin_id',input('id'))->find();
    
        //查询节点信息
        $list = db('role')->select();
           
          

        //获取当前用户的角色信息
        $rolelist = db('admin_role')->where('admin_id',input('id'))->select();
    

            $myrole = array(); //定义空数组
            //对用户的角色进行重组
            foreach($rolelist as $v){
                $myrole[] = $v['role_id'];
            }
            

            //分配数据
            $this->assign("list",$list);
            //分配当前用户信息
            $this->assign('data',$data);
            //分配该用户的角色信息
            $this->assign('role',$myrole);


        return view('Jurisdiction/jobset');
     }

    //保存用户角色
        public function saverole(){
            
            //判读必须选择一个角色
            if(empty($_POST['role'])){
                $this->error("请选择一个角色！");
            }


            //清除用户所有的角色信息，避免重复添加
          db('admin_role')->where('admin_id',input('id'))->delete();
    
            foreach($_POST['role'] as $v){
                $data['admin_id'] = input('id');
                $data['role_id'] = $v;
                //执行添加
                db('admin_role')->data($data)->insert();

            }

            $this->success("角色分配成功",'admin/Jurisdiction/index');
            
        } 
        
   
}



	
